package com.info33.platform.business.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.info33.platform.business.app.dao.AppProtocolDao;
import com.info33.platform.business.app.dto.AppProtocolDTO;
import com.info33.platform.business.app.entity.AppProtocol;
import com.info33.platform.business.app.service.AppProtocolService;
import com.info33.platform.common.constant.TableConstant;
import com.info33.platform.common.mvc.vo.Result;
import com.info33.platform.common.util.SysUtil;
import com.info33.platform.system.vo.SysUserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author admin
 */
@Service
public class AppProtocolServiceImpl extends ServiceImpl<AppProtocolDao, AppProtocol> implements AppProtocolService {

    @Autowired
    private AppProtocolDao appProtocolDao;

    @Override
    public Result<Object> pcEdit(AppProtocol appProtocol) {
        if (Boolean.FALSE.equals(SysUtil.isSysAdmin())) {
            return Result.error(Result.RESULT_ERROR_NO_POWER);
        }
        return this.updateById(appProtocol) ? Result.ok() : Result.error();
    }

    @Override
    public Result<Object> pcRemove(Long id) {
        if (Boolean.FALSE.equals(SysUtil.isSysAdmin())) {
            return Result.error(Result.RESULT_ERROR_NO_POWER);
        }
        return this.removeById(id) ? Result.ok() : Result.error();
    }

    @Override
    public IPage<AppProtocol> pcPage(Page<AppProtocol> objectPage, AppProtocolDTO dto, SysUserVO currentUser) {
        return appProtocolDao.selectPage(objectPage, new QueryWrapper<AppProtocol>()
                .select(TableConstant.ID, "name", "content", TableConstant.UPDATE_DATE, TableConstant.REMARKS, TableConstant.SORT)
                .eq("type", dto.getType())
                .orderByAsc(TableConstant.SORT)
                .orderByDesc(TableConstant.CREATE_DATE));
    }

}